﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using jiaju.Areas.Back.Models.DAL;
using jiaju.Areas.Back.Models.DBModel;
using jiaju.Areas.Back.Models;
namespace jiaju.Areas.Back.Controllers
{
    [Authorize]
    public class DesignerController : Controller
    {
        // GET: Back/Designer
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult GetDesignerList(int page, int limit, string name)
        {
            //调用servers方法
            List<Designer> datalist = DesignerService.getDesigner(page, limit, name);
            //获取count 总条数
            string sql = "select COUNT(*) from dbo.designer";
            int count = (int)DBHelp.ExcuteScalar(sql);
            Object obj = new
            {
                code = 0,
                msg = "请求成功",
                count = count,
                data = datalist

            };
            return Json(obj, JsonRequestBehavior.AllowGet);
        }
        //新增设计师
        public ActionResult Add()
        {
            return View();
        }
        //新增数据
        //参数名可以用类接受
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult AddDel(Designer designer)
        {
            //新增，请求数据库
            int n = DesignerService.addDesigner(designer);
            if (n > 0)
            {
                return Json(new { success = true, msg = "新增成功" });
            }
            else
            {
                return Json(new { success = false, msg = "新增失败" });
            }

        }
        //处理图片上传UploadPhoto
        [HttpPost]
        public ActionResult UploadPhoto()
        {

            HttpPostedFileBase postFile = Request.Files["uplaodImg"];
            if (postFile != null)
            {
                //生成新的文件名
                string newFileName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".png";
                //图片存到哪
                string fullUploadPath = Server.MapPath("~/images");
                //保存图片=路径加名称
                postFile.SaveAs(fullUploadPath + newFileName);
                //返回地址
                return Json(new { success = true, path = "images" + newFileName, msg = "上传成功" });

            }
            else
            {
                return Json(new { success = false, path = "", msg = "上传失败" });
            }

        }
        //编辑轮播图 Edit
        public ActionResult Edit(string Id)
        {
            //通过Id获取数据详情
            Designer des = DesignerService.GetById(Id);
            //把详情数据传到页面
            ViewBag.A = des;
            return View();
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult EditDel(Designer des)
        {
            //新增，请求数据库
            int m = DesignerService.EditDesigner(des);
            if (m > 0)
            {
                return Json(new { success = true, msg = "编辑成功" });
            }
            else
            {
                return Json(new { success = false, msg = "编辑失败" });
            }
        }
        //删除商品分类方法DelArt
        public ActionResult DelArt(String Id)
        {
            int n = DesignerService.DelDesigner(Id);
            if (n > 0)
            {
                return Json(new { success = true, msg = "删除成功" });
            }
            else
            {
                return Json(new { success = false, msg = "删除失败" });
            }
        }
    }
}